<template>
  <div class="layout-pd">
      <zh-table title="" ref="queryTable"size="mini" funcCode="genProjectModify0001" :columns="pageData.tableColumns" highlight-current-row>
        <template #query>
          <zh-form :model="pageData.queryParams" ref="queryFormRef" label-width="80px" :span="6">
            <zh-form-input
                prop="projectName"
                label="项目名"
                v-model="pageData.queryParams.projectName"
            ></zh-form-input>
            <zh-form-input
                prop="projectCode"
                label="项目标识"
                v-model="pageData.queryParams.projectCode"
            ></zh-form-input>
            <zh-form-query-button  @queryClick="refreshTable"  :span="12">
              <zh-button button-type="add" @click="showAdd()" v-func:genProjectModify0004>新增</zh-button>
            </zh-form-query-button>
          </zh-form>
        </template>
        <template #flag_default="{ row }">
          <zh-badge :status="statusMap[row.status].status" :text="statusMap[row.status].text" />
        </template>
        <template #operate="{ row }">
          <zh-button v-func:genProjectModify0002 plain size="small" color="#409EFF" @click="showdetail(row, true)">查看</zh-button>
          <zh-button v-func:genProjectModify0005 plain size="small" color="#E6A23C" @click="showdetail(row, false)">修改</zh-button>
          <zh-button v-func:genProjectModify0005 plain size="small" color="#4ec752" @click="downloadHandle(row,true)">源码下载</zh-button>
          <zh-button v-func:genProjectModify0006 plain size="small" color="#F56C6C" @click="deleteData(row)">删除</zh-button>
        </template>
      </zh-table>
    <zh-dialog style="width: 60%" :title="pageData.title" v-model="pageData.detailVisible">
      <genProjectModifyForm
          ref="detailFormRef"
          :disabled="pageData.showFlag"
          :downloadFlag="pageData.downloadFlag"
          @canner="canner"
          @refreshTable="refreshTable"
          :id="pageData.selectRow.id"
      ></genProjectModifyForm>
      <template #footer>
				<span class="dialog-footer">
					<zh-button @click="pageData.detailVisible = false" type="info">关闭</zh-button>
					<zh-button
              v-if="!pageData.showFlag||pageData.downloadFlag"
              type="primary"
              :loading="pageData.saveLoading"
              :disabled="pageData.saveLoading"
              v-model:btnLoding="pageData.saveLoading"
              @click="save"
          >
						{{pageData.downloadFlag?'下载':'保存'}}
					</zh-button>
				</span>
      </template>
    </zh-dialog>
  </div>
</template>
<script lang="ts" setup name="genProjectModifyIndex">
import { ElMessageBox, ElMessage } from 'element-plus';
import genProjectModifyForm from './component/genProjectModifyForm.vue';

// ------------------接收传入参数(组件)-------------------

// ------------------钩子函数----------------------------
// 初始化
onMounted(() => {
  init();
});

// ------------------定义子组件--------------------------
const queryTable = ref();
const queryFormRef = ref();
const detailFormRef = ref();

const statusMap = {
  0: {
    status: 'error',
    text: '无效',
  },
  1: {
    status: 'processing',
    text: '有效',
  },
};

// ------------------变量区------------------------------
const pageData = reactive({
  advanced: false,
  toolbarConfig: {
    custom: true,
  },
  tableColumns: [
    {
      type: 'seq',
      width: 60,
      fixed: 'left',
      align: 'center',
      title: '序号',
    },
    {
      field: 'id',
      title: '主键',
      visible: false,
      align: 'center',
      minWidth: 80,
    },
    {
      field: 'projectName',
      title: '项目名',
      visible: true,
      align: 'center',
      width: 150,
      sortable: true,
    },
    {
      field: 'projectCode',
      title: '项目标识',
      visible: true,
      align: 'center',
      width: 100,
    },
    {
      field: 'projectPackage',
      title: '项目包名',
      visible: true,
      align: 'center',
      minWidth: 150,
    },
    {
      field: 'projectPath',
      title: '项目路径',
      visible: true,
      align: 'center',
      minWidth: 300,
    },
    {
      field: 'operate',
      title: '操作',
      fixed: 'right',
      align: 'center',
      minWidth: 260,
      slots: {
        // 使用插槽模板渲染
        default: 'operate',
      },
    },
  ],
  queryParams: {} as any,
  dictData: {} as any,
  title: '',
  detailVisible: false,
  showFlag: false,
  selectRow: {} as any,
  saveLoading: false,
  showType: true,
  downloadFlag:false,
});
// ------------------方法区------------------------------
const init = () => {
  refreshTable();
};
// 查询数据字典信息
const getDict = () => {
  const params = {} as any;
  params.dictFields = '';
  $api.common.getBatchDict.post(params).then((res) => {
    pageData.dictData = res.result;
  });
};

//使用子组件方法，关闭弹窗
const canner = (e: any) => {
  pageData.detailVisible = e;
};

const showAdd = () => {
  pageData.title = '新增';
  pageData.detailVisible = true;
  pageData.showFlag = false;
  pageData.downloadFlag=false;
  pageData.selectRow = {exclusions:'.git,.idea,target,logs',modifySuffix:'java,xml,yml,txt'};
};

const showdetail = (row, flag) => {
  if (!flag) {
    pageData.title = '信息修改';
  } else {
    pageData.title = '查看详情';
  }
  pageData.selectRow = row;
  pageData.showFlag = flag;
  pageData.downloadFlag=false;
  pageData.detailVisible = true;
};
const downloadHandle = (row, flag) => {
  pageData.title = '源码下载';
  let params = Object.assign({}, row) as any;
  if(!params.exclusions){
    params.exclusions='.git,.idea,target,logs'
  }
  if(!params.modifySuffix){
    params.modifySuffix='java,xml,yml,txt'
  }
  pageData.selectRow = params;
  
  pageData.showFlag = flag;
  pageData.downloadFlag=true;
  pageData.detailVisible = true;
}
//保存
const save = () => {
  detailFormRef.value.save();
};

//刷新
const refreshTable = () => {
  queryTable.value.refresh(pageData.queryParams);
};
//删除
const deleteData = (row) => {
  ElMessageBox.confirm('是否确认删除？').then(() => {
    let params = {} as any;
    params.id = row.id;
    $api.common.send.post('genProjectModify0006', params).then(() => {
      ElMessage.success('删除成功');
      refreshTable();
    });
  });
};

</script>
<style lang="scss" scoped></style>
